class ManagersController < ApplicationController

  before_filter :authenticate_employee! 

  def employee_list
    @employees = Employee.where("id != ? and is_admin = ? ",current_employee.id,false)
  end

  def change_active_status
    if params[:type] == "employee"
      @user = Employee.find(params[:id])
      check_active_status(@user,true)
    elsif params[:type] == "client"
      @user = Client.find(params[:id])
      check_active_status( @user )
    end
  end

 def convert_to_manager
    if params[:type] == "employee"
      @user = Employee.find(params[:id])
      manager_status(@user,true)
    elsif params[:type] == "client"
      @user = Client.find(params[:id])
      manager_status( @user )
    end
  end

  def client_list
    @clients = current_employee.get_client
  end

  def edit_client 
    @client = Client.find(params[:id])
  end

  def client_update
    @client = Client.find(params[:id])
    respond_to do |format|
      if @client.update_attributes(params[:client])
        format.html { redirect_to :clients, :notice => 'Client was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit_client" }
        format.json { render :json => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  def assign_clients

   @employee = Employee.find(:all, :conditions => ["is_manager = ? && is_admin = ? && is_active=?", false,false,true ] ) 
     @client =  Client.find(:all, :conditions => ["is_active=?",true ] ) 
     if !params[:employee].nil?
      #@clients = params[:clients].empty? ? [] : Client.where("id in (?)", params[:clients])
      if !params[:clients].nil? 
        params[:clients].each  do |client_id|
          client = Client.find(client_id)
          client.employee_id = params[:employee]
          client.save
        end
      end
      redirect_to :root
    end
  end

  def assign_documents
    
    if !params[:client_id].nil?
      client = Client.find(params[:client_id])

   if !client.employee_id.nil?
    
      documents = params[:document_ids].nil? ? Array.new : Document.where("id in (?)", params[:document_ids])
     if !documents.blank?
     copy_url
       #client.documents << documents
        #client.save
        documents.each do |document|
      
          ClientDocument.create(:client_id => params[:client_id], :document_id => document.id,:attachment_file_name=>document.attachment_file_name,:attachment_content_type=>document.attachment_content_type,:attachment_file_size=>document.attachment_file_size,:attachment_updated_at=>document.attachment_updated_at, :status => "Incomplete")
          DocumentNotification.doc_incomplete(client,document).deliver
           flash[:notice] = "Assign Document Successfully"
        end
      elsif documents.blank?
          flash[:alert] = "Please choose document"
       end
   else
      flash[:alert] = "No employee assign to client"
    end

     redirect_to assign_documents_path 
    end
  end

  def status_assign_doc

   client_ids= current_employee.clients.collect(&:id).flatten
    @doc_clients = ClientDocument.where("client_id in (?) ", client_ids)
    #@documents =  Document.where("id in (?)",doc_ids )
  end

   def copy_url

      client = Client.find(params[:client_id])
  
      document = Document.find(params[:document_ids])

      document.each do |doc|
         path_integer =  doc.attachment.to_s
         path =path_integer.split("?").map {|s| s.gsub("?"," ") }
         read_path="#{Rails.root}/public" + path[0] 
          new_path =  "#{Rails.root}/public" + "/#{client.id}" 
         Dir.mkdir("#{new_path}") unless File.exist?("#{new_path}")
         FileUtils.copy("#{read_path}","#{new_path}")

 #  File.open( read_path, "wb") { |f| f.write(new_path) }
       end
   end



  private

  def check_active_status(user,is_employee=false)

    if !user.is_active.nil? && user.is_active == false
      user.is_active = true 
      
    else
      user.is_active = false 
     
    end
    user.save
  end 

  def manager_status(user,is_employee=false)

 		if !user.is_manager.nil? && user.is_manager == false || !user.is_active.nil? && user.is_active == false
      user.is_manager = true 
    else
      user.is_manager = false 
    end
    user.save
  end 





end
